Ext.define("core.house.controller.HouseController", {
	extend : "Ext.app.Controller",
	mixins : {
		suppleUtil : "core.util.SuppleUtil",
		messageUtil : "core.util.MessageUtil",
		formUtil : "core.util.FormUtil"
		,
	},
	init : function() {
		var self = this;
		this.control({

			"housegrid button[ref=ayncXiaoFei]" : {
				click : function(btn) {
					var grid = btn.ownerCt.ownerCt;
					var store = grid.getStore();
					Ext.Msg
							.confirm(
									"同步消费区域信息确认",
									"<center><h3>确定要同步消费系统的区域信息吗？<h3><h3>当前数据库中的区域信息将会被覆盖</h3></center>",
									function(result) {
										if (result == "yes") {
											// 发送ajax请求
											var wait = Ext.Msg
													.wait("正在同步消费系统区域信息...",
															"请等待...");
											var resObj = self.ajax({
												url : "basicinfo/house!synHouses.action"

											});
											if (resObj.success) {
												self.msgbox(resObj.obj);
												store.loadPage(1);
												wait.close();
											} else {
												wait.close();
												Ext.Msg.alert("友情提示",
														resObj.obj);
											}
										}
										return false;
									});
				}
			},

			/**
			 * 删除区域
			 */
			"housegrid button[ref=deleteHouse]" : {
				click : function(btn) {
					var grid = btn.up("panel[xtype=housegrid]");
					var records = grid.getSelectionModel().getSelection();
					if (records.length > 0) {
						var ids = new Array();
						Ext.each(records, function(rec) {
									var pkValue = rec.get('housebh');
									ids.push(pkValue);
								});
						var candeleteresObj = self.ajax({
									url : "basicinfo/house!canDelete.action",
									params : {
										housebhs : ids.join(",")

									}
								});

						if (candeleteresObj.success) {
							// 封装ids数组
							Ext.Msg.confirm("删除区域确认",
									"<center><h3>确定要删除所选的区域吗？<h3></center>",
									function(result) {
										if (result == "yes") {

											// 发送ajax请求
											var resObj = self.ajax({
												url : "basicinfo/house!deleteHouses.action",
												params : {
													housebhs : ids.join(",")

												}
											});
											if (resObj.success) {
												grid.getStore().loadPage(1);
												self.msgbox(resObj.obj);
											} else {
												Ext.Msg.alert('友情提示',
														resObj.obj);
											}
										}

									});
						} else {
							Ext.Msg.alert('友情提示', '该区域下还有房间,请删除房间后再删除区域！');
							return false;
						}
					} else {
						Ext.Msg.alert('友情提示', '请选择要删除的区域');
						return false;
					}
				}
			},

			/**
			 * 添加区域
			 */
			"housegrid button[ref=addHouse]" : {
				click : function(btn) {

					var window = Ext.create('Ext.window.Window', {
								title : '增加区域',
								height : 280,
								width : 400,
								layout : 'fit',
								modal : true,
								items : {
									xtype : 'addhouseform',
									id : 'addhouseform'
								}
							});
					window.show();
					return false;
				}
			},
			/**
			 * 双击进入form，修改信息
			 */
			"housegrid" : {
				itemdblclick : function(_grid, record, item, index, e, eOpts) {
					var window = Ext.create('Ext.window.Window', {
								title : '区域信息',
								height : 280,
								width : 400,
								layout : 'fit',
								modal : true,
								items : {
									xtype : 'houseform',
									id : 'houseform'
								}
							});

					// var grid = Ext.ComponentQuery
					// .query("component[xtype=housegrid]")[0];
					var form = window.down("panel[xtype=houseform]");

					form.loadRecord(record);
					window.show();
					return false;
				}
			},

			"panel[xtype=houseform] button[ref=return]" : {
				click : function(btn) {
					btn.ownerCt.ownerCt.ownerCt.close();
					return false;
				}
			},

			"panel[xtype=houseform] button[ref=savehouse]" : {
				click : function(btn) {
					var deptForm = btn.up("panel[xtype=houseform]");
					var formObj = deptForm.getForm();
					var params = self.getFormValue(formObj);
					if (formObj.isValid()) {

						var resObj2 = self.ajax({
									url : "zone/updateZone",
									params : params
								});
						if (resObj2.success) {
							// self.msgbox(resObj2.obj);
							Ext.ComponentQuery
									.query("panel[xtype=housegrid] component[xtype=pagingtoolbar]")[0]
									.moveFirst();
							btn.ownerCt.ownerCt.ownerCt.close();
							return false;
						} else {
							Ext.Msg.alert("友情提示", resObj2.obj);
							return false;
						}

					} else {
						Ext.Msg.alert('友情提示', "请检查增加区域的数据");
						return false;
					}

					return false;
				}
			},

			"panel[xtype=addhouseform] button[ref=return]" : {
				click : function(btn) {
					btn.ownerCt.ownerCt.ownerCt.close();
					return false;
				}
			},
			//增加区域
			"panel[xtype=addhouseform] button[ref=savehouse]" : {
				click : function(btn) {
					var deptForm = btn.up("panel[xtype=addhouseform]");
					var formObj = deptForm.getForm();
					var params = self.getFormValue(formObj);
					if (formObj.isValid()) {
						var resObj = self.ajax({
									url : "zone/isExist",
									params : params
								});
						if (resObj.success) {
							var resObj2 = self.ajax({
										url : "zone/addZone",
										params : params
									});
							if (resObj2.success) {
								self.msgbox("添加区域成功");
								Ext.ComponentQuery
										.query("panel[xtype=housegrid] component[xtype=pagingtoolbar]")[0]
										.doRefresh();
								btn.ownerCt.ownerCt.ownerCt.close();
								return false;
							} else {
								Ext.Msg.alert("友情提示", "添加区域失败");
								return false;
							}
						} else {
							console.log(resObj) 
							Ext.Msg.alert('友情提示', resObj.obj);
							return false;
						}
					} else {
						Ext.Msg.alert('友情提示', "请检查增加区域的数据");
						return false;
					}

					return false;
				}
			}
		});
	},
	views : ["core.house.view.HouseLayout", "core.house.view.HouseGrid",
			"core.house.view.AddHouseForm", "core.house.view.HouseForm"],
	stores : ["core.house.store.HouseStore"],
	models : ["core.house.model.HouseModel"]
});